package controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import service.MplanService;
import service.PlanService;
import service.impl.PlanServiceImpl;


import entity.Plan;
@Controller
@RequestMapping("/plan/")
public class PlanController {
	
	@Resource
	private PlanService ps;
	@Resource
	private MplanService ms;
	
	@RequestMapping("main")
	public String main(){
		return "main";
	}
	@RequestMapping("list")
	//模糊查询
	public String list(Plan plan,Model model){
		List<Plan> list=ps.getAll(plan);
		 int next=-1;
		 model.addAttribute("list", list);
		 model.addAttribute("next", next);
		 
		return "planlist";
	}
	//获取指定页面
	@RequestMapping("{page}")
	public String page(@PathVariable int page,Model model){
		int size=5;
		Plan plan=new Plan();
		plan.setSize(size);
		plan.setPage(size*page);
		int pages=ps.pages(size);
		if(page==-2){plan.setPage((pages-1)*size);}
		List<Plan> list=ps.getPage(plan);
		plan.setPage(plan.getPage()/size);
		int next=0;
		if(pages==plan.getPage()+1){
			next=3;
		}
		model.addAttribute("plan", plan);
		model.addAttribute("list", list);
		model.addAttribute("pages", pages);
		model.addAttribute("next", next);
		return "planlist";
	}
	@RequestMapping("add")
	public String add(){
		return "planadd";
	}
	@RequestMapping(value="addsave")
	public String addsave(Plan plan,Model model){
		String meg=null;
		if(ps.addPlan(plan)){
			meg="添加成功！";
		}else{meg="添加失败！";}
		model.addAttribute("meg", meg);
		return "redirect:/plan/-2";
	}
	@RequestMapping("delete/{id}")
	public String delete(@PathVariable int id,Model model){
		String meg=null;
		if(ps.delete(id)){
			meg="删除成功！";
		}else{meg="删除失败！";}
		model.addAttribute("meg", meg);
		return "main";
	}
	
	@RequestMapping("detail/{id}")
	public String detail(@PathVariable("id") int id,Model model){
		Plan plan=ps.one(id);
		model.addAttribute(plan);
		return "plandetail";
	}
	
	@RequestMapping("one/{id}")
	public String update(@PathVariable("id") int id,Model model){
		Plan plan=ps.one(id);
		model.addAttribute(plan);
		return "planupdate";
	}
	@RequestMapping("update")
	public String updatesave(Plan plan,Model model){
		String meg=null;
		if(ps.update(plan)){
			meg="更新成功！";
		}else{meg="更新失败！";}
		model.addAttribute("meg", meg);
		return "main";
	}
}
